package com.nic.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.util.Anticlockwise;


/**
 * Created by Administrator on 2018/8/22.
 */

public class VoiceDialog extends Dialog {

    private final ImageView ivLogo;
    private final ImageView imageView;
    private final Anticlockwise btn_clock;
    private final TextView mPrompt;

    public VoiceDialog(Context context) {
        super(context, R.style.dialog_no_background);
        setContentView(R.layout.popupwindow_voice);
        setCanceledOnTouchOutside(false);
        ivLogo = findViewById(R.id.iv_logo);
        imageView = (ImageView) findViewById(R.id.imageView);
        btn_clock = (Anticlockwise) findViewById(R.id.btn_clock);
        mPrompt = findViewById(R.id.tv_prompt);
        btn_clock.setTimeFormat("ss");
        btn_clock.bindAnimationView(imageView);
//        //设置toast的View
//        setView(parent);
//        //弹出位置
//        setGravity(Gravity.CENTER, 0, 0);
//        //时长
//        setDuration(Toast.LENGTH_SHORT);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
//        animationDrawable.start();
    }

    public void reset() {
        imageView.setVisibility(View.VISIBLE);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
//        animationDrawable.start();
        btn_clock.setText(null);
        ivLogo.setImageResource(R.mipmap.dialog_popup_mic);
        mPrompt.setText("手指上滑，取消发送");
    }

    /**
     * 获取倒计时控件
     *
     * @return
     */
    public Anticlockwise getAnticlockwise() {
        return btn_clock;
    }

    public void setPrompt(String prompt) {
        mPrompt.setText(prompt);
    }

    public void setLogo(@DrawableRes int id) {
        ivLogo.setImageResource(id);
    }

    public void setDecibelLevel(double db) {
        if (db < 70) {
            db -= 30;
            if (db <= 0) {
                db = (int) (10 + Math.random() * (40 - 10 + 1));
            }
        } else if (db < 80) {
            db -= 15;
            if (db <= 0) {
                db = (int) (10 + Math.random() * (40 - 10 + 1));
            }
        }
        imageView.getDrawable().setLevel(((int) db + 10) * 100);
    }

    //    public void showLong() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(0);
//            }
//        }).start();
//    }
//
//    public void dismissLong() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(1);
//            }
//        }).start();
//
//    }
//
//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == 0) {
//                show();
//                sendEmptyMessageDelayed(0, 1000);
//            } else if (msg.what == 1) {
//                //取消
//                if (hasMessages(0)) {
//                    removeMessages(0);
//                }
//            }
//        }
//    };
}
